<?php

class Menu extends CActiveRecord
{
	private $tmp_menu = array();
	private $tmp_menu_items = array();
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{menu}}';
	}

	public function rules()
	{
		return array(
			array('label, title', 'required'),
			array('label', 'length', 'max'=>50),
			array('title', 'length', 'max'=>255),
			array('id, label, title', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
			'items' => array(self::HAS_MANY, 'Menu_items', 'menu_id', 'order'=>'items.parent ASC, items.sorter ASC'),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => tt('ID'),
			'label' => tt('Label'),
			'title' => tt('Title'),
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('label',$this->label,true);
		$criteria->compare('title',$this->title,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>param('adminPaginationPageSize', 20),
			),
		));
	}
	
	public function getMenu($label)
	{
		if (param('cacheEnabled'))
		{
			$menu = Yii::app()->cache->get($label);
			if (empty($menu))
			{
				Menu::model()->updateCache($label);
				$menu = Yii::app()->cache->get($label);
			}
		}
		else
		{
			$criteria=new CDbCriteria;
			$criteria->compare('label',$label);
			$criteria->order = 'sorter DESC';

			$menu = Menu::model()->find($criteria)->buildMenu();
		}
		
		return $menu;
	}
	
	private function buildMenu($parentId=0)
	{
		$this->tmp_menu = $this->tmp_menu_items = array();
		if (is_array($this->items))
		{
			foreach ($this->items as $i)
			{
				if ($i->active == '1')
				{
					$el = array(
						'id' => $i->id,
						'parent' => $i->parent,
						'active' => $i->active,
						'sorter' => $i->sorter,
						'route' => $i->route,
					);
					$languages = Languages::model()->getLanguages();
					foreach ($languages as $l)
					{
						$el['title_'.$l['code']] = $i->{'title_'.$l['code']};
					}
					$this->tmp_menu_items[$i->id] = $el;
				}
			}
			
			foreach ($this->tmp_menu_items as $i)
			{
				$this->tmp_menu[$i['parent']][] = $i;
			}

			return $this->generateMenu($parentId);
		}
		return false;
	}
	
	private function generateMenu($parentId)
	{
		if(empty($this->tmp_menu) || empty($this->tmp_menu[$parentId]))
		{
			return array();
		}

		$menu = array();
		foreach($this->tmp_menu[$parentId] as $subitem)
		{
			if(isset($this->tmp_menu[$subitem["id"]]) && !empty($this->tmp_menu[$subitem["id"]]))
			{
				$subitem["items"] = $this->generateMenu($subitem["id"]);
			}
			$menu[] = $subitem;
		}
		return $menu;
	}	
	
	public function updateCache($label)
	{
		$criteria=new CDbCriteria;
		$criteria->compare('label',$label);

		$m = Menu::model()->find($criteria)->buildMenu();
		Yii::app()->cache->set($label, $m);
	}
	
	public function afterSave()
	{
		if (param('cacheEnabled'))
			$this->updateCache($this->label);
		
		return parent::afterSave();
	}
}